package com.jacky.webflux.commons;

import java.io.Serializable;
import java.util.Objects;

public class BasicResp<T> implements Serializable {

    final long serialVersionUID = 42L;

    T data;

    Integer code;

    String msg;

    public BasicResp(T data, Integer code, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public static <T> BasicResp<T> success(T data) {
        return new BasicResp<>(data, 200, "success");
    }

    public static <T> BasicResp<T> fail(String msg) {
        return new BasicResp<>(null, 100, msg);
    }

    public static <T> BasicResp<T> fail(Integer code, String msg) {
        return new BasicResp<>(null, code, msg);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BasicResp<?> basicResp = (BasicResp<?>) o;
        return Objects.equals(data, basicResp.data) && Objects.equals(code, basicResp.code) && Objects.equals(msg, basicResp.msg);
    }

    @Override
    public int hashCode() {
        return Objects.hash(data, code, msg);
    }
}
